warning('off','MATLAB:singularMatrix')
k=getenv('SLURM_ARRAY_TASK_ID');
kk=str2num(k(1));
version=str2num(k(2));

addpath /scratch/akalandr/SUPPLEMENTAL/CIRC
addpath /scratch/akalandr/SUPPLEMENTAL/Combined
addpath /scratch/akalandr/SUPPLEMENTAL/Inference
addpath /scratch/akalandr/SUPPLEMENTAL/TwoStepMatlab

pc=parcluster('local')
JOB_ID=getenv('SLURM_JOBID')
pc.JobStorageLocation=strcat('/local_scratch/',JOB_ID)
parpool(pc,24)

cd /scratch/akalandr/SUPPLEMENTAL/CIRC
load('GERN.mat')
cd /scratch/akalandr/SUPPLEMENTAL/RESULTS
years = unique(cellfun(@(x) x(2:end),fieldnames(GERN),'UniformOutput', false));

Dname = strcat('M',years{kk});
D = GERN.(Dname);

Pname = strcat('P',years{kk});
P = GERN.(Pname);


Pind=1:length(P);

PL = Pind(matches(P,'SPD'));
PR = Pind(matches(P,'CDU'));

if Pind(matches(P,'CSU'))>6
    the6=[1 2 7 4 5 6];
else
    the6=1:6;
end


Fname = strcat(Dname,'6v',int2str(version));
Est = EstimationCS(D(:,the6),PL,PR,version,1);
save(Fname,'Est')
if length(P)==6
    Fname = strcat(Dname,'Fv',int2str(version));
    save(Fname,'Est')
end


delete(gcp('nocreate'))
exit

